{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Concepts \n",
    "\n",
    "The core idea of agents is to use a language model to choose a sequence of actions to take. In chains, a sequence of actions is hardcoded (in code). In agents, a language model is used as a reasoning engine to determine which actions to take and in which order.\n",
    "\n",
    "代理的核心思想是使用语言模型来选择要执行的一系列操作。在链中，一系列动作是硬编码的（在代码中）。在代理中，语言模型用作推理引擎，以确定要执行的操作以及执行的顺序。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Schema\n",
    "LangChain has several abstractions to make working with agents easy.\n",
    "\n",
    "LangChain有几个抽象概念，使得与代理一起工作变得容易。\n",
    "\n",
    "- AgentAction 代理操作\n",
    "\n",
    "This is a dataclass that represents the action an agent should take. It has a tool property (which is the name of the tool that should be invoked) and a tool_input property (the input to that tool)\n",
    "\n",
    "这是一个数据类，表示代理应执行的操作。它有一个tool属性（即应调用的工具的名称）和一个tool_input属性（该工具的输入）\n",
    "\n",
    "- AgentFinish 代理完成\n",
    "\n",
    "This represents the final result from an agent, when it is ready to return to the user. It contains a return_values key-value mapping, which contains the final agent output. Usually, this contains an output key containing a string that is the agent's response.\n",
    "\n",
    "这表示当代理准备好返回给用户时的最终结果。它包含一个return_values键值映射，其中包含最终的代理输出。通常，它包含一个output键，其中包含一个字符串，该字符串是代理的响应。\n",
    "\n",
    "- Intermediate Steps 中间步骤\n",
    "\n",
    "These represent previous agent actions and corresponding outputs from this CURRENT agent run. These are important to pass to future iteration so the agent knows what work it has already done. This is typed as a List[Tuple[AgentAction, Any]]. Note that observation is currently left as type Any to be maximally flexible. In practice, this is often a string.\n",
    "\n",
    "它们表示以前的代理操作以及此 CURRENT 代理运行的相应输出。这些对于传递给未来的迭代非常重要，这样代理才能知道它已经完成了哪些工作。它被类型化为 List[Tuple[AgentAction, Any]]。请注意，观察值当前保留为“Any”类型，以实现最大的灵活性。在实践中，这通常是一个字符串。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Agent\n",
    "This is the chain responsible for deciding what step to take next. This is usually powered by a language model, a prompt, and an output parser.\n",
    "\n",
    "这是负责决定下一步采取什么步骤的链。这通常由语言模型、提示符和输出解析器提供支持。\n",
    "\n",
    "Different agents have different prompting styles for reasoning, different ways of encoding inputs, and different ways of parsing the output. For a full list of built-in agents see agent types. You can also easily build custom agents, should you need further control.\n",
    "\n",
    "不同的代理具有不同的推理提示样式，不同的输入编码方式以及不同的输出解析方式。有关内置代理的完整列表，请参阅代理类型。如果需要进一步控制，您还可以轻松构建自定义代理。\n",
    "\n",
    "- Agent Inputs 代理输入\n",
    "The inputs to an agent are a key-value mapping. There is only one required key: intermediate_steps, which corresponds to Intermediate Steps as described above.\n",
    "\n",
    "代理的输入是键值映射。只有一个必需的键：intermediate_steps，它对应于上述的Intermediate Steps。\n",
    "\n",
    "Generally, the PromptTemplate takes care of transforming these pairs into a format that can best be passed into the LLM.\n",
    "\n",
    "通常，PromptTemplate 负责将这些对转换为最适合传递给 LLM 的格式。\n",
    "\n",
    "- Agent Outputs 代理输出\n",
    "The output is the next action(s) to take or the final response to send to the user (AgentActions or AgentFinish). Concretely, this can be typed as Union[AgentAction, List[AgentAction], AgentFinish].\n",
    "\n",
    "输出是要执行的下一个操作或要发送给用户的最终响应（AgentActions 或 AgentFinish）。具体来说，这可以类型化为 Union[AgentAction, List[AgentAction], AgentFinish]\n",
    "\n",
    "The output parser is responsible for taking the raw LLM output and transforming it into one of these three types.\n",
    "\n",
    "输出解析器负责获取原始 LLM 输出并将其转换为这三种类型之一。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## AgentExecutor\n",
    "The agent executor is the runtime for an agent. This is what actually calls the agent, executes the actions it chooses, passes the action outputs back to the agent, and repeats. In pseudocode, this looks roughly like:\n",
    "\n",
    "代理执行器是代理的运行时。这就是实际调用代理的内容，执行它选择的操作，将操作输出传回代理，然后重复。在伪代码中，这大致如下所示：\n",
    "\n",
    "```python\n",
    "next_action = agent.get_action(...)\n",
    "while next_action != AgentFinish:\n",
    "    observation = run(next_action)\n",
    "    next_action = agent.get_action(..., next_action, observation)\n",
    "return next_action\n",
    "```\n",
    "While this may seem simple, there are several complexities this runtime handles for you, including:\n",
    "1. Handling cases where the agent selects a non-existent tool\n",
    "2. Handling cases where the tool errors\n",
    "3. Handling cases where the agent produces output that cannot be parsed into a tool invocation\n",
    "4. Logging and observability at all levels (agent decisions, tool calls) to stdout and/or to LangSmith.\n",
    "\n",
    "虽然这看起来很简单，但此运行时会为您处理几个复杂性，包括：\n",
    "1. 处理代理选择不存在的工具的情况\n",
    "2. 工具出错情况的处理\n",
    "3. 处理代理生成的输出无法解析为工具调用的情况\n",
    "4. 将所有级别（代理决策、工具调用）的日志记录和可观察性记录到 stdout 和/或 LangSmith。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tools\n",
    "Tools are functions that an agent can invoke. The Tool abstraction consists of two components:\n",
    "\n",
    "工具是代理可以调用的函数。Tool抽象由两个组件组成：\n",
    "\n",
    "1. The input schema for the tool. This tells the LLM what parameters are needed to call the tool. Without this, it will not know what the correct inputs are. These parameters should be sensibly named and described.\n",
    "\n",
    "工具的输入方案。这告诉 LLM 需要哪些参数来调用该工具。没有这个，它就不会知道正确的输入是什么。这些参数应该被合理地命名和描述。\n",
    "\n",
    "2. The function to run. This is generally just a Python function that is invoked.\n",
    "\n",
    "要运行的函数。这通常只是一个被调用的 Python 函数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Toolkits\n",
    "For many common tasks, an agent will need a set of related tools. For this LangChain provides the concept of toolkits - groups of around 3-5 tools needed to accomplish specific objectives. For example, the GitHub toolkit has a tool for searching through GitHub issues, a tool for reading a file, a tool for commenting, etc.\n",
    "\n",
    "对于许多常见任务，代理将需要一组相关工具。为此，LangChain提供了工具包的概念 - 完成特定目标所需的大约3-5个工具组。例如，GitHub 工具包有一个用于搜索 GitHub 问题的工具、一个用于读取文件的工具、一个用于评论的工具等。\n",
    "\n",
    "LangChain provides a wide set of toolkits to get started. For a full list of built-in toolkits, see the toolkits integrations section\n",
    "\n",
    "LangChain提供了一套广泛的工具包供您入门。有关内置工具包的完整列表，"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
